// 2013-9-11下午11:03:46  net.betou.trade.main.manager.role.impl.SystemRoleMngImpl.java

package net.betou.trade.main.manager.role.impl;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import net.betou.common.hibernate3.Updater;
import net.betou.common.hibernate3.Updater.UpdateMode;
import net.betou.core.service.RoleListener;
import net.betou.trade.entity.main.AgentAndAdmins;
import net.betou.trade.entity.main.Role;
import net.betou.trade.main.dao.role.SystemRoleDao;
import net.betou.trade.main.manager.role.SystemRoleMng;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * @author 叶装装
 */
@Service
@Transactional
public class SystemRoleMngImpl implements SystemRoleMng{

	@Transactional( readOnly = true )
	public List< Role > list( AgentAndAdmins admin, String searchProperty, String searchValue, String orderProperty, String orderDirection ) {
		return dao.findList( true, null, admin.getSearchSettingRole( ).getLevel( ), searchProperty, searchValue, orderProperty, orderDirection );
	}
	
	@Transactional( readOnly = true )
	public List< Role > listToAdmin( AgentAndAdmins admin ){
		return dao.findList( true, false, admin.getSearchSettingRole( ).getLevel( ), null, null, null, null );
	}
	
	@Transactional( readOnly = true )
	public List< Role > listByIdentity( String identity ){
		return dao.getListByIdentity( identity  );
	}
	
	@Transactional( readOnly = true )
	public Role findRole( Integer id ){
		return dao.findByManager( true, id );
	}
	
	public Role save( Role bean, Set< String > perms ){
		bean.saveToSystem( );
		bean.setPerms( perms );
		dao.save( bean );
		return bean;
		
	}
	
	/**
	 * @version 1.0 
	 * allowInit 默认就让他为false
	 */
	public Role update( String description, Integer level, String name, Role bean, Set< String > perms ){
		return update( false, description, level, name, bean, perms );
	}
	
	public Role update( Boolean allowInit, String description, Integer level, String name, Role bean, Set< String > perms ){
		bean.update( allowInit, description, level, name );
		
		bean.setPerms( perms );
		
		Updater< Role > updater = new Updater< Role >( bean );
		
		updater.exclude( "roleDefault" ); // 不更新是否默认
		updater.exclude( "allowRead" );
		updater.exclude( "identity" ); // 不包括这三种
		updater.exclude( "admin" );
		bean = dao.updateByUpdater( updater );
		
		afterUpdate( bean, perms );
		
		return bean;
	}
	
	public Role update( Role bean, Set< String > perms ){
		Updater< Role > updater = new Updater< Role >( bean, UpdateMode.MIN );
		updater.include( "perms" );
		
		bean.setPerms( perms );
		
		bean = dao.updateByUpdater( updater );
		
		return bean;
	}
	
	public Role [ ] deleteByIds( Integer [ ] ids ){
		Role [ ] beans = new Role [ ids.length ];
		for ( int i = 0, len = ids.length; i < len; i++ ){
			beans[ i ] = dao.deleteById( ids[ i ] );
		}
		return beans;
	}
	
	private void afterUpdate( Role bean, Set< String > perms ) {

		for( RoleListener r : listenerList ){
			r.afterUpdate( bean, perms );
		}
	}
	
	public Role [] findListByAdminIds ( Integer [ ] adminIds ){
		HashSet<Role> rs = new HashSet<Role>();
		if(adminIds!=null){
			for(Integer id : adminIds){
				List<Role> list = dao.findList(null, id);
				rs.addAll(list);
			}
		}
		return rs.toArray(new Role[rs.size()]);
	}

	private List< RoleListener > listenerList;
	
	public void setListenerList( List< RoleListener > listenerList ) {
		this.listenerList = listenerList;
	}

	@Autowired
	private SystemRoleDao dao;
	
}
